﻿@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>区域类别管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="~/Scripts/layui/css/layui.css" rel="stylesheet" />
    <style>
        .layui-table-cell {
            height: auto;
            white-space: normal;
        }

        .area-type-form {
            padding: 20px 30px 0 0;
        }
    </style>
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-header">区域类别管理</div>
            <div class="layui-card-body">
                <!-- 搜索栏 -->
                <form class="layui-form" style="margin-bottom: 20px;">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">类别名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="AreaTypeName" placeholder="请输入区域类别名称" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="search">查询</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            <button type="button" class="layui-btn layui-btn-warm" onclick="addAreaType()">添加区域类别</button>
                        </div>
                    </div>
                </form>

                <!-- 区域类别表格 -->
                <table class="layui-hide" id="areaTypeTable" lay-filter="areaTypeTable"></table>
            </div>
        </div>
    </div>

    <!-- 表格操作列模板 -->
    <script type="text/html" id="toolbar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
    </script>

    <!-- 添加/编辑区域类别表单模板 -->
    <script type="text/html" id="areaTypeFormTemplate">
        <form class="layui-form area-type-form" lay-filter="areaTypeForm">
            <input type="hidden" name="AreaTypeID" id="AreaTypeID">

            <div class="layui-form-item">
                <label class="layui-form-label">类别名称</label>
                <div class="layui-input-block">
                    <input type="text" name="AreaTypeName" required lay-verify="required|AreaTypeName" placeholder="请输入区域类别名称" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">备注说明</label>
                <div class="layui-input-block">
                    <textarea name="AreaTypeMark" placeholder="请输入备注信息" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item layui-hide">
                <button type="button" class="layui-btn" lay-submit lay-filter="submitBtn" id="submitBtn">提交</button>
            </div>
        </form>
    </script>

    <script src="~/Scripts/jquery-3.7.0.min.js"></script>
    <script src="~/Scripts/layui/layui.js"></script>
    <script>
        var table, form, layer;

        // 初始化layui模块
        layui.use(['table', 'form', 'layer'], function () {
            table = layui.table;
            form = layui.form;
            layer = layui.layer;

            // 渲染表格
            var tableIns = table.render({
                elem: '#areaTypeTable',
                url: '/AreaType/GetAreaTypeList',
                method: 'post',
                page: true,
                limit: 10,
                limits: [10, 20, 30, 50],
                cols: [[
                    { field: 'AreaTypeID', title: '类别ID', width: 80, sort: true },
                    { field: 'AreaTypeName', title: '类别名称', width: 200 },
                    { field: 'AreaTypeMark', title: '备注说明', minWidth: 300 },
                    { fixed: 'right', title: '操作', toolbar: '#toolbar', width: 120 }
                ]],
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    msgName: 'msg',
                    countName: 'count',
                    dataName: 'data'
                },
                parseData: function (res) {
                    return {
                        "code": res.code,
                        "msg": res.msg,
                        "count": res.count,
                        "data": res.data
                    };
                }
            });

            // 搜索功能
            form.on('submit(search)', function (data) {
                table.reload('areaTypeTable', {
                    where: data.field,
                    page: {
                        curr: 1
                    }
                });
                return false;
            });

            // 监听工具条
            table.on('tool(areaTypeTable)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {
                    editAreaType(data.AreaTypeID);
                } else if (obj.event === 'delete') {
                    deleteAreaType(data.AreaTypeID, data.AreaTypeName);
                }
            });
        });

        // 添加区域类别
        function addAreaType() {
            var formHtml = $('#areaTypeFormTemplate').html();

            var index = layer.open({
                type: 1,
                title: '添加区域类别',
                content: formHtml,
                area: ['500px', '300px'],
                success: function (layero, index) {
                    // 表单验证
                    form.verify({
                        AreaTypeName: function (value) {
                            if (value.length > 50) {
                                return '区域类别名称不能超过50个字符';
                            }
                            // 检查区域类别名称是否已存在
                            var isUnique = true;
                            $.ajax({
                                url: '/AreaType/CheckAreaTypeName',
                                type: 'POST',
                                data: {
                                    areaTypeName: value
                                },
                                async: false,
                                dataType: 'json',
                                success: function (res) {
                                    if (res.code === 1) {
                                        isUnique = false;
                                    }
                                },
                                error: function () {
                                    isUnique = false;
                                }
                            });
                            if (!isUnique) {
                                return '区域类别名称已存在，请使用其他名称';
                            }
                        }
                    });

                    // 监听表单提交
                    form.on('submit(submitBtn)', function (data) {
                        submitAreaTypeForm(data.field, 'add');
                        return false;
                    });
                },
                btn: ['保存', '取消'],
                yes: function (index, layero) {
                    // 触发提交按钮
                    $('#submitBtn').click();
                }
            });
        }

        // 编辑区域类别
        function editAreaType(id) {
            var formHtml = $('#areaTypeFormTemplate').html();

            var index = layer.open({
                type: 1,
                title: '编辑区域类别',
                content: formHtml,
                area: ['500px', '300px'],
                success: function (layero, index) {
                    // 加载区域类别信息
                    loadAreaTypeInfo(id);

                    // 表单验证
                    form.verify({
                        AreaTypeName: function (value) {
                            if (value.length > 50) {
                                return '区域类别名称不能超过50个字符';
                            }
                            // 检查区域类别名称是否已存在（排除当前区域类别）
                            var isUnique = true;
                            $.ajax({
                                url: '/AreaType/CheckAreaTypeName',
                                type: 'POST',
                                data: {
                                    areaTypeName: value,
                                    areaTypeId: id
                                },
                                async: false,
                                dataType: 'json',
                                success: function (res) {
                                    if (res.code === 1) {
                                        isUnique = false;
                                    }
                                },
                                error: function () {
                                    isUnique = false;
                                }
                            });
                            if (!isUnique) {
                                return '区域类别名称已存在，请使用其他名称';
                            }
                        }
                    });

                    // 监听表单提交
                    form.on('submit(submitBtn)', function (data) {
                        submitAreaTypeForm(data.field, 'edit');
                        return false;
                    });
                },
                btn: ['保存', '取消'],
                yes: function (index, layero) {
                    // 触发提交按钮
                    $('#submitBtn').click();
                }
            });
        }

        // 加载区域类别信息
        function loadAreaTypeInfo(id) {
            // 从表格数据中获取区域类别信息
            var tableData = table.cache.areaTypeTable;
            var areaType = tableData.find(function(item) {
                return item.AreaTypeID === id;
            });

            if (areaType) {
                form.val('areaTypeForm', {
                    "AreaTypeID": areaType.AreaTypeID,
                    "AreaTypeName": areaType.AreaTypeName,
                    "AreaTypeMark": areaType.AreaTypeMark || ''
                });
            } else {
                layer.msg('加载区域类别信息失败', { icon: 2 });
            }
        }

        // 提交区域类别表单
        function submitAreaTypeForm(formData, type) {
            var url = type === 'add' ? '/AreaType/AddAreaType' : '/AreaType/UpdateAreaType';

            $.ajax({
                url: url,
                type: 'POST',
                data: formData,
                dataType: 'json',
                success: function (res) {
                    if (res.code === 0) {
                        layer.msg(type === 'add' ? '添加成功' : '修改成功', { icon: 1 });
                        layer.closeAll();
                        table.reload('areaTypeTable');
                    } else {
                        layer.msg(res.msg, { icon: 2 });
                    }
                },
                error: function () {
                    layer.msg('请求失败', { icon: 2 });
                }
            });
        }

        // 删除区域类别
        function deleteAreaType(id, name) {
            layer.confirm('确定要删除区域类别 "' + name + '" 吗？', function (index) {
                $.ajax({
                    url: '/AreaType/DeleteAreaType',
                    type: 'POST',
                    data: { id: id },
                    dataType: 'json',
                    success: function (res) {
                        if (res.code === 0) {
                            layer.msg('删除成功', { icon: 1 });
                            table.reload('areaTypeTable');
                        } else {
                            layer.msg(res.msg, { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg('请求失败', { icon: 2 });
                    }
                });
                layer.close(index);
            });
        }
    </script>
</body>
</html>